package com.sun.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.dto.TagAndAmount;
import com.sun.pojo.Blog;
import com.sun.service.BlogService;
import com.sun.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/8
 */
@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tags/{id}")
    public String showTag(@PathVariable("id") Long id, Model model,
                          @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        List<TagAndAmount> tags=tagService.selectTagAndAmount();
        if (id == -1) {
            id = tags.get(0).getTagId();
        }
        PageHelper.startPage(pageNum, 3);
        List<Blog> blogByTag = blogService.selectBlogByTag(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogByTag);
        model.addAttribute("tags", tags);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("tagId", id);
        return "tags";
    }



}

